const exp = require('express'),
    Crawler = require('crawler'),
    fs = require('fs'),
    bodyParser = require('body-parser'),
    cookieParser = require('cookie-parser'),
    app = exp();

app.use(exp.static('wwwroot'));
app.use(bodyParser.urlencoded({ extended: true }));
app.engine('html', require('express-art-template'));
app.set('views engine', 'html');
app.use(cookieParser());

app.get('/', (req, res) => {
    var data = fs.readFileSync('jingdong/catedata.json').toString();
    var rank = fs.readFileSync('jingdong/rank.json').toString();
    var enjoy = fs.readFileSync('jingdong/enjoy.json').toString();
    var life = fs.readFileSync('jingdong/aishenghuo.json').toString();
    var more = fs.readFileSync('jingdong/moreinner.json').toString();
    data = JSON.parse(data);
    rank = JSON.parse(rank);
    enjoy = JSON.parse(enjoy);
    life = JSON.parse(life);
    more = JSON.parse(more);
    var file = {};
    file.data = data;
    file.rank = rank;
    file.enjoy = enjoy;
    file.life = life;
    file.more = more;
    res.render('index.html', { data: file })
})
app.get('/api/register', (req, res) => {
    res.render('register.html')
})
app.get('/api/login', (req, res) => {
    res.render('login.html')
})

app.get('/api/nav', (req, res) => {
    var data = fs.readFileSync('jingdong/wj.json').toString();
    data = JSON.parse(data);
    res.status(200).json({
        code: 'success',
        data: data
    })
})
app.get('/api/jdms', (req, res) => {
    var data = fs.readFileSync('jingdong/jdmiaosha.json').toString();
    data = JSON.parse(data);
    res.status(200).json({
        code: 'success',
        data: data
    })
})
app.get('/api/jydq', (req, res) => {
    var data = fs.readFileSync('jingdong/catedata.json').toString();
    data = JSON.parse(data);
    res.status(200).json({
        code: 'success',
        data: data
    })
})

app.use(require('./router/regist'));
app.use(require('./router/login'));

app.listen(3000, () => {
    console.log('server on 3000....')
})